#include "prase.h"
#include <QDebug>
#include <QDateTime>
Prase::Prase(QObject *parent) : QObject(parent)
{

}

void Prase::parseHeader(const char *src, QString &orderName, int &len)
{
    auto size = new char[2];
    orderName = QString::fromLatin1(src,5);
    memcpy(size,src+5,5);
    auto size1 = QString::fromLatin1(size,2);
    len = size1.toInt(nullptr,16);
    qDebug()<<__FUNCTION__<<"name = "<<orderName<<"len = "<<len<<QByteArray(src).toHex();
}

PowerInfo Prase::parseGLJC(QByteArray data)
{
    PowerInfo info;
    info.endPointHex =  QString::fromLatin1(data.data()+7,3);
    QByteArray arr;
    info.power1 = arr.append(data.at(10)).toHex().toInt(nullptr,16);
    arr.clear();
    info.power2 = arr.append(data.at(11)).toHex().toInt(nullptr,16);
    arr.clear();
    info.power3 = arr.append(data.at(12)).toHex().toInt(nullptr,16);
    arr.clear();
    info.power4 = arr.append(data.at(13)).toHex().toInt(nullptr,16);
    arr.clear();
    info.power5 = arr.append(data.at(14)).toHex().toInt(nullptr,16);
    arr.clear();
    info.power6 = arr.append(data.at(15)).toHex().toInt(nullptr,16);
    info.updateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
    return info;
}

GPSInfo Prase::parseGPS(QByteArray data)
{
    GPSInfo info;
    QByteArray arr;
    QString time ;
    int  timeCache;
    info.userPointHex =  QString::fromLatin1(data.data()+7,3);
    arr.clear();
    arr.append(data.at(10));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"") +QString::number(timeCache) + "年");
    arr.clear();
    arr.append(data.at(11));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "月");
    arr.clear();
    arr.append(data.at(12));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ "日");
    arr.clear();
    arr.append(data.at(13));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "时");
    arr.clear();
    arr.append(data.at(14));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ "分");
    arr.clear();
    arr.append(data.at(15));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "秒");
    info.time = time;


    time.clear();
    arr.clear();
    arr.append(data.at(16));
    time.append(arr.data());
    arr.clear();
    arr.append(data.at(17));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "度");
    arr.clear();
    arr.append(data.at(18));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ "分");
    arr.clear();
    arr.append(data.at(19));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ ".");
    arr.clear();
    arr.append(data.at(20));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "秒");
    info.longitude = time;

    time.clear();
    arr.clear();
    arr.append(data.at(21));
    time.append(arr.data());
    arr.clear();
    arr.append(data.at(22));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ "度");
    arr.clear();
    arr.append(data.at(23));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ "分");
    arr.clear();
    arr.append(data.at(24));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"" ) +QString::number(timeCache)+ ".");
    arr.clear();
    arr.append(data.at(25));
    timeCache = arr.toHex().toInt(nullptr,16);
    time.append((timeCache < 10 ? "0":"")  +QString::number(timeCache)+ "度");
    info.latitude = time;

    arr.clear();
    arr.append(data.at(26));
    arr.append(data.at(27));
    info.hight = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(28));
    arr.append(data.at(29));
    info.speed = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(30));
    arr.append(data.at(31));
    info.direction = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(32));
    info.sateLiteNum = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(33));
    info.status = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(34));
    info.precision = arr.toHex().toInt(nullptr,16);
    arr.clear();
    arr.append(data.at(35));
    arr.append(data.at(36));
    info.deviation = arr.toHex().toInt(nullptr,16);
    info.updateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
    return info;
}

CommunicationInfo Prase::parseCommunication(QByteArray data)
{
    CommunicationInfo info;
    info.userPointHex =  QString::fromLatin1(data.data()+7,3);
    QByteArray arr;
    info.msgType = arr.append(data.at(10)).toHex();
    arr.clear();
    info.senderEndPointHex = arr.append(data.at(11)).append(data.at(12)).append(data.at(13)).toHex();
    arr.clear();
    info.sendTime = "0000";
    info.recInfomationSize = arr.append(data.at(16)).append(data.at(17)).toHex();
    arr.clear();
    QString recInfomation;
    int infoSize = arr.append(data.at(16)).append(data.at(17)).toHex().toInt(nullptr,16)/8;
    for(int i = 0 ; i < infoSize ; i++){
        info.recInfomation.append(data.at(18 + i));
    }
    arr.clear();
    info.CRCStatus = arr.append(data.at(18 + infoSize)).toHex();
    info.updateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
    return info;
}
